// pages/noticeList/index.js
var util = require('../../utils/util.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowClass: 0,
    offset: 0,
    limit:10,
    classList: [],
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
  },

  changClass(e) {
    this.data.offset = 0;
    this.data.list = [];
    this.setData({
      nowClass: e.currentTarget.dataset.id
    })
    this.init();
  },
  init() {
    let that = this;
    util.http({
      url: 'api/miniwechat/index/news',
      data: {
        offset: this.data.offset,
        limit: this.data.limit,
        channel_id: this.data.nowClass
      }
    }).then((data) => {
      data.list.forEach((item,index)=>{
        WxParse.wxParse('item' + index, 'html', item.content, that);
        item.contentText = this.data['item' + index]
        this.data.list.push(item);
      })
      this.setData({
        classList: data.channel,
        list: this.data.list,
        total: data.total
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  onReachBottom: function () {
    if (this.data.total > this.data.offset + 1 * this.data.limit) {
      this.data.offset += 1;
      this.init();
    }
  },
  goNoticeDetail(e) {
    wx.navigateTo({
      url: '/pages/noticeDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
})